// releasePage/pages/homePageInfo/networkType/networkType.js
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        ipxBottom: 0,
        companyContent: [],
        operatorArr: [],
        operatorIndexArr: [],
        operatorIndex: 0,
        typeArr: [],
        typeIndex: Number,
        operator: '',
        networktype: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        this.setData({
            operator: options.operator,
            networktype: options.networktype,
        });
        this.getOperatorList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    operatorSelect(e) {
        this.setData({
            operatorIndex: e.currentTarget.dataset.index,
            typeIndex: Number,
        });
    },
    typeSelect(e) {
        this.setData({
            typeIndex: e.currentTarget.dataset.index,
        });
    },
    // 获取运营商列表
    getOperatorList() {
        let _that = this;
        let country;
        if (app.globalData.countryBack == '中国') {
            if (app.globalData.addressBack == '香港') {
                country = '中国香港';
            } else if (
                app.globalData.taiwan.includes(app.globalData.addressBack)
            ) {
                country = '中国台湾';
            } else if (app.globalData.addressBack == '澳门') {
                country = '中国澳门';
            } else {
                country = '中国大陆';
            }
        } else {
            country = app.globalData.countryBack;
        }
        let data = {
            authId: app.globalData.authId,
            pageSize: 100,
            pageNo: 1,
            country: country,
        };
        http.getRequest(`cjMap/getCityOperatorList`, data).then((res) => {
            console.log(res);
            for (let i = 0; i < res.data.length; i++) {
                res.data[i].checked = false;
                let tempNetworkSupportArr = [];
                if (res.data[i].networksupport.includes('00')) {
                    tempNetworkSupportArr.push('NR');
                }
                if (res.data[i].networksupport.includes('01')) {
                    tempNetworkSupportArr.push('ViWiFi');
                }
                if (res.data[i].networksupport.includes('02')) {
                    tempNetworkSupportArr.push('VoWiFi');
                }
                if (res.data[i].networksupport.includes('03')) {
                    tempNetworkSupportArr.push('ViLTE');
                }
                if (res.data[i].networksupport.includes('04')) {
                    if (res.data[i].networksupport.includes('05')) {
                        tempNetworkSupportArr.push('TD/FDD-LTE');
                    } else {
                        tempNetworkSupportArr.push('TD-LTE');
                    }
                } else if (res.data[i].networksupport.includes('05')) {
                    tempNetworkSupportArr.push('FDD-LTE');
                }
                if (res.data[i].networksupport.includes('06')) {
                    tempNetworkSupportArr.push('VoLTE');
                }
                if (res.data[i].networksupport.includes('07')) {
                    tempNetworkSupportArr.push('WCDMA');
                }
                if (res.data[i].networksupport.includes('08')) {
                    tempNetworkSupportArr.push('CDMA2000');
                }
                if (res.data[i].networksupport.includes('09')) {
                    tempNetworkSupportArr.push('TD-SCDMA');
                }
                if (res.data[i].networksupport.includes('10')) {
                    tempNetworkSupportArr.push('GSM');
                }
                if (res.data[i].networksupport.includes('11')) {
                    tempNetworkSupportArr.push('CDMA1x');
                }
                _that.data.typeArr.push(tempNetworkSupportArr);
                _that.data.operatorArr.push(res.data[i].operator);
                _that.data.operatorIndexArr.push(res.data[i].id);
                if (res.data[i].operator == _that.data.operator) {
                    for (let i = 0; i < tempNetworkSupportArr.length; i++) {
                        if (
                            tempNetworkSupportArr[i] == _that.data.networktype
                        ) {
                            _that.setData({
                                typeIndex: i,
                            });
                        }
                    }
                    _that.setData({
                        operatorIndex: i,
                    });
                }
            }
            _that.setData({
                typeArr: _that.data.typeArr,
                operatorArr: _that.data.operatorArr,
                operatorIndexArr: _that.data.operatorIndexArr,
            });
        });
    },
    backTap(e) {
        if (this.data.typeArr[this.data.operatorIndex][this.data.typeIndex]) {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.setData({
                operator: this.data.operatorArr[this.data.operatorIndex],
                ftLineOperatorId: this.data.operatorIndexArr[
                    this.data.operatorIndex
                ],
                networktype: this.data.typeArr[this.data.operatorIndex][
                    this.data.typeIndex
                ],
            });
            wx.navigateBack({
                delta: 1, // 返回上一级页面。
            });
        } else {
            wx.showToast({
                title: '请将信息填写完整',
                icon: 'none',
                duration: 1500,
            });
        }
    },
});
